/***** Serendipity Booksellers -- bookdata.cpp *****/

#include "stdafx.h"

#include <iostream>
#include <iomanip>
#include "bookdata.h"

/*************************************************************************************
 *  Serialization scheme for the BookData Class:                                     *
 *	int length of bookTitle: bookTitle.data()                                        *
 *	int length of isbn: isbn.data()                                                  *
 *	int length of author: author.data()                                              *
 *	int length of publisher: publisher.data()                                        *
 *	int length of dataAdded: dateAdded.data()                                        *
 *	int length of qtyOnHand                                                          *
 *	double wholesale                                                                 *
 *	double retail                                                                    *
 *	bool empty                                                                       *
 *************************************************************************************/

/*************************************************************************************
 *                       writeString                                                 *
 *	Helper function for serialization of BookData                                    *
 *************************************************************************************/
void writeString(fstream &file, string &str)
{
	unsigned int len = str.length();
	file.write(reinterpret_cast<char *>(&len), sizeof(len));
	file.write(str.data(), len);
}

/*************************************************************************************
 *                       BookData::store                                             *
 *	BookData serialization function.                                                 *
 *************************************************************************************/
void BookData::store(fstream &file)
{
	writeString(file, bookTitle);
	writeString(file, isbn);
	writeString(file, author);
	writeString(file, publisher);
	writeString(file, dateAdded);
	file.write(reinterpret_cast<char *>(&qtyOnHand), sizeof(qtyOnHand));
	file.write(reinterpret_cast<char *>(&wholesale), sizeof(wholesale));
	file.write(reinterpret_cast<char *>(&retail), sizeof(retail));
}

/*************************************************************************************
 *                       readString(fstream)                                         *
 *	BookData deserialization helper function.                                        *
 *************************************************************************************/
const int MAXSTRLEN = 256; // Max length of string used for title or name etc.
void readString(fstream &file, string &str)
{
	static char buffer[MAXSTRLEN];
	int strlength;
	file.read(reinterpret_cast<char *>(&strlength), sizeof(strlength));
	file.read(buffer, strlength);
	// Null terminate the string and then turn it into a string object
	buffer[strlength] = 0;
	str = buffer;
}

/*************************************************************************************
 *                       BookData::load                                              *
 *	BookData deserialization function.                                               *
 *************************************************************************************/
void BookData::load(fstream &file)
{
	readString(file, bookTitle);
	readString(file, isbn);
	readString(file, author);
	readString(file, publisher);
	readString(file, dateAdded);
	file.read(reinterpret_cast<char *>(&qtyOnHand), sizeof(qtyOnHand));
	file.read(reinterpret_cast<char *>(&wholesale), sizeof(wholesale));
	file.read(reinterpret_cast<char *>(&retail), sizeof(retail));
}

/*************************************************************************************
 *                       BookData()                                                  *
 *	Default contructor                                                               *
 *	Initializes member data to null/zero.                                            *
 *************************************************************************************/
BookData::BookData()
{
	qtyOnHand = 0;
	wholesale = retail = 0.0;
	bookTitle = isbn = author = publisher = dateAdded = "";
}

/*************************************************************************************
 *                       setTitle()                                                  *
 *	Copies the book title to the title data member.                                  *
 *************************************************************************************/
void BookData::setTitle(string title_in)
{
	bookTitle = title_in;
}

/*************************************************************************************
 *                       setIsbn()                                                   *
 *	Copies the book ISBN to the isbn data member.                                    *
 *************************************************************************************/
void BookData::setIsbn(string isbn_in)
{
	isbn = isbn_in;
}

/*************************************************************************************
 *                       setAuthor()                                                 *
 *	Copies the author's name to the author data member.                              *
 *************************************************************************************/
void BookData::setAuthor(string author_in)
{
	author = author_in;
}

/*************************************************************************************
 *                       setPub()                                                    *
 *	Copies the book publisher to the publisher data member.                          *
 *************************************************************************************/
void BookData::setPub(string publisher_in)
{
	publisher = publisher_in;
}

/*************************************************************************************
 *                       setDateAdded()                                              *
 *	Copies the date to the dateAdded data member.                                    *
 *************************************************************************************/
void BookData::setDateAdded(string date_in)
{
	dateAdded = date_in;
}

/*************************************************************************************
 *                       setQty()                                                    *
 *	Copies the quantity to the qtyOnHand data member.                                *
 *************************************************************************************/
void BookData::setQty(int quantity_in)
{
	qtyOnHand = quantity_in;
}

/*************************************************************************************
 *                       setWholesael()                                              *
 *	Copies wholesale cost to the wholesale data member                               *
 *************************************************************************************/
void BookData::setWholesale(double wholesale_in)
{
	wholesale = wholesale_in;
}

/*************************************************************************************
 *                       setRetail()                                                 *
 *	Copies retail price to the retail data member                                    *
 *************************************************************************************/
void BookData::setRetail(double retail_in)
{
	retail = retail_in;
}

/*************************************************************************************
 *                       getTitle()                                                  *
 *	Returns value stored in data member bookTitle.                                   *
 *************************************************************************************/
string BookData::getTitle()
{
	return bookTitle;
}

/*************************************************************************************
 *                       getIsbn()                                                   *
 *	Returns valuse store in data member isbn.                                        *
 *************************************************************************************/
string BookData::getIsbn()
{
	return isbn;
}

/*************************************************************************************
 *                       getAuthor()                                                 *
 *	Returns valuse store in data member author.                                      *
 *************************************************************************************/
string BookData::getAuthor()
{
	return author;
}

/*************************************************************************************
 *                       getPub()                                                    *
 *	Returns valuse store in data member publisher.                                   *
 *************************************************************************************/
string BookData::getPub()
{
	return publisher;
}

/*************************************************************************************
 *                       getDateAdded()                                              *
 *	Returns valuse store in data member dateAdded.                                   *
 *************************************************************************************/
string BookData::getDateAdded()
{
	return dateAdded;
}

/*************************************************************************************
 *                       getQty()                                                    *
 *	Returns valuse store in data member qtyOnHand                                    *
 *************************************************************************************/
int BookData::getQty()
{
	return qtyOnHand;
}

/*************************************************************************************
 *                       getWholesale()                                              *
 *	Returns valuse store in data member wholesale.                                   *
 *************************************************************************************/
double BookData:: getWholesale()
{
	return wholesale;
}

/*************************************************************************************
 *                       getRetail()                                                 *
 *	Returns valuse store in data member retail                                       *
 *************************************************************************************/
double BookData::getRetail()
{
	return retail;
}

/*************************************************************************************
 *                       bookInfo                                                    *
 *	Displays information about a particular book.                                    *
 *************************************************************************************/
void BookData::bookInfo()	// Receives a BookData object
{
	// Display company name and screen title
	cout << "\n\n\n		Serendipity Booksellers\n";
	cout << "			Book Information\n\n\n";

	cout << fixed << showpoint << setprecision(2);

	// Display information items
	cout << "	ISBN:			" << getIsbn() << endl;
	cout << "	Title:			" << getTitle() << endl;
	cout << "	Author:			" << getAuthor() << endl;
	cout << "	Publisher:		" << getPub() << endl;
	cout << "	Date Added:		" << getDateAdded() << endl;
	cout << "	Qty on Hand:		" << getQty() << endl;
	cout << "	Wholesale Cost:		" << getWholesale() << endl;
	cout << "	Retail Price:		" << getRetail() << endl << endl;
}

/*************************************************************************************
 *                       bookReturn()                                                *
 *	Returns BookData object to be deleted.                                           *
 *************************************************************************************/
BookData BookData::bookReturn()
{
	BookData removeBook;
	removeBook.setIsbn(getIsbn());
	removeBook.setTitle(getTitle());
	removeBook.setAuthor(getAuthor());
	removeBook.setPub(getPub());
	removeBook.setDateAdded(getDateAdded());
	removeBook.setQty(getQty());
	removeBook.setWholesale(getWholesale());
	removeBook.setRetail(getRetail());
	return removeBook;
}